package main

import (
	"enca/controller"
	"enca/module/db"

	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

//跨域
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", origin)
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
			c.Header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization")
			c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Cache-Control, Content-Language, Content-Type")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}

func init() {
	db.Open()

	// shaizi.Open()
}

func main() {

	//创建路由
	r := gin.Default()
	//通配跨域
	r.Use(Cors())

	//管理组
	admin := r.Group("/blogAdmin")
	//博客组
	blog := r.Group("/blog")
	//画板组
	drawing := r.Group("/drawing")

	admin.POST("/login", controller.AdminLogin)
	admin.POST("/uploadImage", controller.AdminUploadImage)
	admin.GET("/list", controller.AdminBlogList)
	admin.POST("/detail", controller.AdminBlogDetail)
	admin.POST("/add", controller.AdminBlogAdd)
	admin.POST("/del", controller.AdminBlogDel)
	admin.POST("/edit", controller.AdminBlogEdit)
	admin.POST("/online", controller.AdminBlogOnline)
	admin.GET("/config", controller.AdminBlogConfig)
	admin.POST("/configUpdate", controller.AdminBlogConfigUpdate)

	drawing.POST("/del", controller.AdminDrawingDel)

	blog.GET("/list", controller.Blogs)
	blog.POST("/commentAdd", controller.CommentAdd)
	blog.POST("/detail", controller.BlogDetail)
	blog.POST("/uploadImageDrawing", controller.UploadImageDrawing)
	blog.GET("/drawings", controller.Drawings)
	blog.GET("/indexConfig", controller.IndexConfig)

	r.Run(":9090")

}
